<?php

namespace App\Http\Controllers\Admin;

use Validator;
use App\ProductType;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ProductTypeController extends Controller
{
    public function index(Request $request)
    {
        $productTypes = ProductType::orderBy('sort', 'asc')->get();
        return view('admin.product_type.index', [
            'request' => $request,
            'productTypes' => $productTypes
        ]);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'name' => 'bail|required|min:1',
                'sort' => 'bail|required|integer'
            ];
            $messages = [
                'name.required' => '请输入分类名！',
                'name.min' => '分类名最少1个字符！',
                'sort.required' => '请输入排序值！',
                'sort.integer' => '请输入正确的排序值！'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                $return = [
                    'code' => __LINE__,
                    'msg' => $validator->errors()->first()
                ];
                return response()->json($return);
            }
            $productType = new ProductType();
            $productType->name = $data['name'];
            $productType->sort = $data['sort'];
            $productType->save();
            $return = ['code' => 0];
            return response()->json($return);
        }
        return view('admin.product_type.add', [
            'request' => $request
        ]);
    }

    public function edit(Request $request, $id)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'name' => 'bail|required|min:1',
                'sort' => 'bail|required|integer'
            ];
            $messages = [
                'name.required' => '请输入分类名！',
                'name.min' => '分类名最少1个字符！',
                'sort.required' => '请输入排序值！',
                'sort.integer' => '请输入正确的排序值！'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                $return = [
                    'code' => __LINE__,
                    'msg' => $validator->errors()->first()
                ];
                return response()->json($return);
            }
            $productType = ProductType::find($data['id']);
            $productType->name = $data['name'];
            $productType->sort = $data['sort'];
            $productType->save();
            $return = ['code' => 0];
            return response()->json($return);
        }
        $productType = ProductType::find($id);
        return view('admin.product_type.edit', [
            'request' => $request,
            'productType' => $productType
        ]);
    }

    public function del(Request $request)
    {
        $id = $request->input('id');
        $productType = ProductType::find($id);
        if ($productType->delete()) {
            $return = ['code' => 0];
        } else {
            $return = [
                'code' => __LINE__,
                'msg' => '删除失败，请稍候重试！'
            ];
        }
        return response()->json($return);
    }
}